var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
Page({
  data: {
    //是否采用衔接滑动  
    circular: true,
    //是否显示画板指示点  
    indicatorDots: true,
    //选中点的颜色  
    indicatorcolor: "#000",
    //是否竖直  
    vertical: false,
    //是否自动切换  
    autoplay: true,
    //自动切换的间隔
    interval: 2500,
    //滑动动画时长毫秒
    duration: 100,
    //所有图片的高度  （必须）
    imgheights: [],
    //图片宽度 
    imgwidth: 750,
    //默认  （必须）
    current: 0,
    currentTab: 0,
    info: [],
    imgUrl: app.globalData.imgUrl,
    localImg: app.globalData.staticimgUrl,  // 本地图片
    goodsid: 1,
    goodsmoney: 0, // 预约金
    storemoney: 0, // 到点再付
    replyCommentId:'',
    replyPeople:'',
    replyContent:'',
    star:''
  },

  onLoad: function(options) {
    app.showLoading();
    const that = this;
    this.setData({
      goodsid: options.id,
      etype: options.etype
    });
 
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
      
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        that.loadData();
      } ,
      fail: function (e) {
        console.log(e);
      },
    })
  },
  // 加载数据
  loadData: function () {
  
    var that = this;
    let url;
    let { latitude, longitude, goodsid } = this.data;
    let lngandlat = longitude + ',' + latitude;
    app.request({
      url: `shopp/yimeiGoodsApi/getDetail/${goodsid}`,
      method: "GET",
      data: {
        lngandlat: lngandlat
      },
      success(res) {
        wx.hideLoading();
        that.setData({
          info: res.data,
          goodsmoney: res.data.goodsDetail.reservations,
          storemoney: res.data.goodsDetail.storePayment,
          star: Math.round(res.data.doctor.averageScore)
        })
      }
    })
  },
  imageLoad: function (e) {
    //获取图片真实宽度
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比
      ratio = imgwidth / imgheight;
    //计算的高度值
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight
    var imgheights = this.data.imgheights
    //把每一张图片的高度记录到数组里
    imgheights.push(imgheight)
    this.setData({
      imgheights: imgheights,
    })
  },
  bindchange: function (e) {
    this.setData({
      current: e.detail.current
    })
  },
  //滑动切换
  swiperTab: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
    if (e.detail.current == 1) {
      this.loadDetail();
    }
  },
  // 预览大图
  previewImage(e) {
    const that = this;
    const urls = [];
    const imgUrl = this.data.imgUrl;
    const current = e.currentTarget.dataset.src;
    const commentId = e.currentTarget.dataset.commentid;
    const imgArr = this.data.info.comment.commentArray;
   
    for (let i = 0; i < imgArr.length; i++) {
      if (imgArr[i].commentId == commentId){
        for (let j = 0; j < imgArr[i].commentEnclosures.length; j++) {
          urls.push(imgUrl + imgArr[i].commentEnclosures[j])
        }
      }
    }
   
    wx.previewImage({
      current,
      urls
    })
  },

  //点击切换
  clickTab: function (e) {
    const current = e.target.dataset.current;
    if (0 == current) {
      this.setData({
        currentTab: current
      })
    }
    if (1 == current) {
      this.setData({
        currentTab: current
      })
      this.loadDetail();
      // this.loadData()
    }

  },
  loadDetail: function(e) {
    let { latitude, longitude, goodsid, etype } = this.data;
    var that = this;
    var url = '';
    if (etype == 1) {
      url = 'shopp/yimeiGoodsApi/getGoodsDetailsHtml/' + goodsid;
    } else {
      url = 'shopp/goodsApi/getGoodsDetailsHtml/' + goodsid;
    }
   
    app.request({
      url: url,
      method:"GET",
      success(res) {
        var detail = res.data.photo;
        that.setData({
          goodsDetailsHtml: res.data.photo,
        })
        WxParse.wxParse('detail', 'html', detail, that, 5);
      }
    })
  },
  //去订单
  gobuy: function(e) {
    console.log(e);
    var goodsmoney = e.currentTarget.dataset.money;
    var goodsid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../user-shop-goods-order-1/user-shop-goods-order-1?money=' + goodsmoney,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  //去质询
  goconsult: function() {
    var isLogin = wx.getStorageSync('isLogin');
    if (isLogin != 2) {
      wx.navigateTo({
        url: '../user-my-kf/user-my-kf',
      })
    } else if (isLogin == 1) {
      wx.reLaunch({
        url: '/pages/doctor-login/doctor-login',
      });
    }
  },
  // 关注按钮
  bindFollow: function(e) {
    var userid = e.currentTarget.dataset.userid;
    var that = this;
    app.request({
      url: 'doctor/member/attention',
      data: {
        userId: userid,
      },
      success(res) {
        app.showToast(res.message)
        that.loadData();
      }
    })
  },
  checkReplyCommment: function(e){
    let replyCommentId = e.currentTarget.dataset.commentId;
    let replyPeople = e.currentTarget.dataset.replyPeople;
    this.setData({
      replyCommentId,
      replyPeople:`回复${replyPeople}`
    });
  },
  changeReplyContent: function(e){
    let replyContent = e.detail.value;
    this.setData({
      replyContent
    });
  },
  bindReply: function(){
    const that = this;
    let { replyCommentId, replyContent } = this.data;
    if(replyCommentId){
      if (replyContent){
        app.request({
          url:'shopp/goodsCommentApi/replyComment',
          data:{
            commentid: replyCommentId,
            replycontent: replyContent
          },
          success: function(res){
            wx.showToast({
              title: '回复完成',
              icon: 'none',
              duration: 1000,
              success: function(){
                that.loadData();
                that.clearReplyData();
              }
            });
          }
        })
      }
      else{
        wx.showToast({
          title: '请输入回复内容',
          icon: 'none',
          duration: 1000
        });
      }
    }
    else{
      wx.showToast({
        title: '请选择需要回复的评价',
        icon:'none',
        duration:1000
      });
    }
  },
  clearReplyData:function(){
    this.setData({
      replyCommentId: '',
      replyPeople: '',
      replyContent: ''
    });
  }
})